<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//公司管理
class Company extends CommonController {

	function __construct(){
		
		parent::__construct();
		$this->load->model('User/companyModel','CurModel');
		
	}
	
	
	//start
	/**
	* @name 获取列表
	* @param page ture char 页面
	* @param size ture char 数量
	* @return 成功返回1
	*/
	//end
	function get_list(){
		$user_id = $this->user_id;
		
		$filter = array('name');
		$arr = array(
			'filter'=>$filter
		);
		
		$result = parent::_get_list($arr);
		if(!$result){
			$this->err_out($this->getError());
		}
		
		$this->data_out($result);
	}
	
	
	//start
	/**
	* @name 获取全部
	* @return 成功返回1
	*/
	//end
	function get_all(){
		
		$list = array();
		//添加缓存开始，ling添加，20191012
		if(file_exists(APPPATH."cache/all_company_cache.php")){
			include(APPPATH."cache/all_company_cache.php");

			if($company_cache){
				$list = $company_cache;
			}
		}else{
			$list = $this->CurModel->getAll(array('is_enable'=>1),"*",0,0,'sort','desc');
		}
		
		$arr = array();
		
		
		$querys = explode(',',$this->querys);
		
		foreach($list as $key=>$val){
			if(in_array($val['id'],$querys)){
				$arr[] = $this->info_out($val);
			}
			
		}
		
		$this->data_out($arr);
	}
	
	
	function info_out($info){
		
		return $info;
	}
	
	//start
	/**
	* @name 获取信息
	* @param id ture char ID
	* @return 成功返回1
	*/
	//end
	function get_info(){
		$user_id = $this->user_id;
		
		$result = parent::_get_info();
		if(!$result){
			$this->err_out($this->getError());
		}
		
		$this->data_out($result);
	}
	
	//start
	/**
	* @name 获取信息
	* @param id ture char ID
	* @return 成功返回1
	*/
	//end
	function save(){
		
		if(!$this->check_priv('company_update|company_add')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$filter = array(
			'name'=>'请输入名称',
			'sx_name'=>'请输入缩写名称'
		);
		$post = array('name','sx_name','sort','is_enable');
		$farr = array(
			'post'=>$post,
			'filter'=>$filter
		);
		
		$result = parent::_save($farr);
		if(!$result){
			$this->err_out($this->getError());
		}

		//写入缓存
		$rel = $this->CurModel->getAll(array('is_enable'=>1),"*",0,0,'sort','desc');
		parent::cache_write('company_cache',$rel);
		
		$this->add_log("保存主体公司【{$result['id']}】信息",'bumen');
		$this->data_out($result);
	}
	
	
	
	//start
	/**
	* @name 删除
	* @param id ture int ID
	* @return 成功返回1
	*/
	//end
	function del(){
		
		if(!$this->check_priv('company_del')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$result = parent::_del();
		if(!$result){
			$this->err_out($this->getError());
		}

		//写入缓存
		$rel = $this->CurModel->getAll(array('is_enable'=>1),"*",0,0,'sort','desc');
		parent::cache_write('company_cache',$rel);
		
		$this->add_log("删除主体公司【{$result}】",'bumen');
		$this->data_out($result);
		
	}
	
	//start
	/**
	* @name 批量操作
	* @param ids ture int_array ID组合
	* @param act ture int 操作类型enable，disconnected，delete
	* @return 成功返回1
	*/
	//end
	function batch_act(){
		$ids = $this->input->post('ids',null,array());
		$act = $this->input->post('act',null,'');
		
		$act_arr = array('enable','disconnected','delete');
		if(!in_array($act,$act_arr)){
			$this->err_out('错误的错误');
		}
		if(!is_array($ids)){
			$ids = explode(',',$ids);
		}
		if(count($ids)<=0){
			$this->err_out('请选择成员');
		}
		$ls_ids = array();
		foreach($ids as $id){
			if(!empty($id)){
				$ls_ids[] = $id;
			}
		}
		
		if($act=='enable'){
			$this->CurModel->edit(array('is_enable'=>1),"id in(".implode(',',$ls_ids).")");
		}
		elseif($act=='disconnected'){
			$this->CurModel->edit(array('is_enable'=>0),"id in(".implode(',',$ls_ids).")");
		}
		elseif($act=='delete'){
			$this->CurModel->del("id in(".implode(',',$ls_ids).")");
		}

		//写入缓存
		$rel = $this->CurModel->getAll(array('is_enable'=>1),"*",0,0,'sort','desc');
		parent::cache_write('company_cache',$rel);

		$this->data_out(1);
	}
	//start
	/**
	* @name 设置是否启用
	* @param id ture int ID
	* @param status ture int 状态
	* @return 成功返回1
	*/
	//end
	function set_enable(){
		$id = $this->input->post('id',null,0);
		$status = $this->input->post('status',null,0);
		$info = $this->CurModel->get_info($id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		$this->CurModel->edit(array('is_enable'=>$status),"id ='{$id}'");
		
		$this->data_out(1);
	}
	
}
